//
//  LCCardTaskApplyViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/4/7.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCCardTaskApplyViewController.h"
#import "LCTaskService.h"
#import "LCTaskModel.h"
#import "LCUserModel.h"

#import "LCSetAddressAreaView.h"

@interface LCCardTaskApplyViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nameTfl;

@property (weak, nonatomic) IBOutlet UITextField *cardNoTfl;

@property (weak, nonatomic) IBOutlet UILabel *areaLb;

@property (weak, nonatomic) IBOutlet UITextField *addressTfl;

@property (weak, nonatomic) IBOutlet UITextField *phoneTfl;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *codeViewH;

@property (weak, nonatomic) IBOutlet UITextField *codeTfl;


@property (strong, nonatomic)NSDictionary * setAreaDic;

@end

@implementation LCCardTaskApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}

-(void)configUI {
    self.codeViewH.constant = self.task.smsCode? 77 : 0;
}

- (IBAction)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)setAreaBtnAction:(UIButton *)sender {
    [self.view endEditing:YES];
    NSString * province = @"";
    NSString * city = @"";
    if (self.setAreaDic[@"province"]) {
        province = self.setAreaDic[@"province"];
    }
    if (self.setAreaDic[@"city"]) {
        city = self.setAreaDic[@"city"];
    }
    
    LCSetAddressAreaView * areaView = [[LCSetAddressAreaView alloc] initWithStyle:LCAddressStyleProvinceCityArea Province:province City:city Block:^(NSDictionary *dic) {
        self.setAreaDic = dic;
        self.areaLb.text = [NSString stringWithFormat:@"%@%@%@",dic[@"province"], dic[@"city"], dic[@"area"]];
        self.areaLb.textColor = [UIColor colorWithHexString:@"#222222"];
    }];
    [areaView show];
}

- (IBAction)sendCodeAction:(UIButton *)sender {
    if (self.nameTfl.text.length <= 0) {
        [self showErrorText:self.nameTfl.placeholder];
    } else if (self.cardNoTfl.text.length <= 0) {
        [self showErrorText:self.cardNoTfl.placeholder];
    } else if (![NSString isIdCardNumber:self.cardNoTfl.text]) {
        [self showErrorText:@"请输入正确的身份证件号码"];
    } else if (self.phoneTfl.text.length <= 0) {
        [self showErrorText:self.phoneTfl.placeholder];
    } else if (![NSString isMobileNumber:self.phoneTfl.text]) {
        [self showErrorText:@"请输入正确的手机号码"];
    } else {
        [self showText:@"获取验证码中..."];
        NSMutableDictionary * param = [NSMutableDictionary dictionary];
        param[@"goodsId"] = [NSString stringWithFormat:@"%ld",self.task.id];
        param[@"certName"] = self.nameTfl.text;
        param[@"certNo"] = self.cardNoTfl.text;
        param[@"contactPhone"] = self.phoneTfl.text;
        param[@"promoCode"] = @"1OWBZf6K";
        
        [LCTaskService sendCodeWithParam:param Block:^(BOOL isSuccess) {
            if (isSuccess) {
                [sender startWithTime:60 title:@"重新获取" countDownTitle:@"s" mainColor:nil countColor:nil];
                [self dismissLoading];
            }
        }];
    }
}

- (IBAction)submitBtnAction:(UIButton *)sender {
    if (self.nameTfl.text.length <= 0) {
        [self showErrorText:self.nameTfl.placeholder];
    } else if (self.cardNoTfl.text.length <= 0) {
        [self showErrorText:self.cardNoTfl.placeholder];
    } else if (![NSString isIdCardNumber:self.cardNoTfl.text]) {
        [self showErrorText:@"请输入正确的身份证件号码"];
    } else if ([self.areaLb.text isEqualToString:@"请选择省市区"]) {
        [self showErrorText:@"请选择省市区"];
    } else if (self.addressTfl.text.length <= 0) {
        [self showErrorText:self.addressTfl.placeholder];
    } else if (self.phoneTfl.text.length <= 0) {
        [self showErrorText:self.phoneTfl.placeholder];
    } else if (![NSString isMobileNumber:self.phoneTfl.text]) {
        [self showErrorText:@"请输入正确的手机号码"];
    } else if (self.task.smsCode == 1 && self.codeTfl.text <= 0) {
        [self showErrorText:self.codeTfl.placeholder];
    } else {
        NSMutableDictionary * param = [NSMutableDictionary dictionary];
        param[@"promoCode"] = @"1OWBZf6K";
        param[@"certName"] = self.nameTfl.text;
        param[@"certId"] = self.cardNoTfl.text;
        param[@"contractPhone"] = self.phoneTfl.text;
        param[@"province"] = self.setAreaDic[@"provinceCode"];
        param[@"city"] = self.setAreaDic[@"cityCode"];
        param[@"county"] = self.setAreaDic[@"areaCode"];
        param[@"provinceName"] = self.setAreaDic[@"province"];
        param[@"cityName"] = self.setAreaDic[@"city"];
        param[@"countyName"] = self.setAreaDic[@"area"];
        param[@"address"] = self.addressTfl.text;
        param[@"goodsId"] = kFORMAT(@"%ld",self.task.id);
        
        param[@"customNo"] = kFORMAT(@"%@%@", [NSString GetTimeIntervalWithDate:[NSDate date]], LCUserDefGet(Userid));
        if (self.task.smsCode == 1) {
            param[@"captcha"] = self.codeTfl.text;
        }
        NSString * callbackUrl = [NSString stringWithFormat:@"%@/haoka/u%@.aspx",XWXDomainName,LCUserDefGet(Userid)];
        param[@"callbackUrl"] = callbackUrl;
        NSLog(@"%@",callbackUrl);
        
        [LCTaskService cardApplyWithParam:param Block:^(BOOL isSuccess) {
            if (isSuccess) {
                [self showSuccessText:@"申请成功"];
                [self.navigationController popViewControllerAnimated:YES];
//                [self saveCardPrice];
            }
        }];
    }
}

-(void)saveCardPrice {
    NSString * goodId = kFORMAT(@"%ld",self.task.id);
    LCUserModel * userInfo = [LCUserModel mj_objectWithKeyValues:LCUserDefGet(UserInfo)];
    CGFloat price = self.task.commission.floatValue * self.point * (100-userInfo.cardrate.integerValue)/100.0;
    NSString * amount = kFORMAT(@"%.2f",price);
    [LCTaskService saveCardPriceWithGoodId:goodId Amount:amount Block:^(BOOL isSuccess) {
        if (isSuccess) {
            [self showSuccessText:@"申请成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

@end
